package cn.iocoder.yudao.module.promotion.controller.admin.interviewlog;

import cn.iocoder.yudao.module.promotion.controller.admin.interviewlog.vo.*;
import cn.iocoder.yudao.module.promotion.convert.interviewlog.InterviewLogConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.interviewlog.InterviewLogDO;
import cn.iocoder.yudao.module.promotion.service.interviewlog.InterviewLogService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

@Tag(name = "管理后台 - url访问记录")
@RestController
@RequestMapping("/promotion/interview-log")
@Validated
public class InterviewLogController {

    @Resource
    private InterviewLogService interviewLogService;

    @PostMapping("/create")
    @Operation(summary = "创建url访问记录")
    @PreAuthorize("@ss.hasPermission('zfkj:interview-log:create')")
    public CommonResult<Long> createInterviewLog(@Valid @RequestBody InterviewLogCreateReqVO createReqVO) {
        return success(interviewLogService.createInterviewLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新url访问记录")
    @PreAuthorize("@ss.hasPermission('zfkj:interview-log:update')")
    public CommonResult<Boolean> updateInterviewLog(@Valid @RequestBody InterviewLogUpdateReqVO updateReqVO) {
        interviewLogService.updateInterviewLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除url访问记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zfkj:interview-log:delete')")
    public CommonResult<Boolean> deleteInterviewLog(@RequestParam("id") Long id) {
        interviewLogService.deleteInterviewLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得url访问记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zfkj:interview-log:query')")
    public CommonResult<InterviewLogRespVO> getInterviewLog(@RequestParam("id") Long id) {
        InterviewLogDO interviewLog = interviewLogService.getInterviewLog(id);
        return success(InterviewLogConvert.INSTANCE.convert(interviewLog));
    }

    @GetMapping("/list")
    @Operation(summary = "获得url访问记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('zfkj:interview-log:query')")
    public CommonResult<List<InterviewLogRespVO>> getInterviewLogList(@RequestParam("ids") Collection<Long> ids) {
        List<InterviewLogDO> list = interviewLogService.getInterviewLogList(ids);
        return success(InterviewLogConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得url访问记录分页")
    @PreAuthorize("@ss.hasPermission('zfkj:interview-log:query')")
    public CommonResult<PageResult<InterviewLogRespVO>> getInterviewLogPage(@Valid InterviewLogPageReqVO pageVO) {
        PageResult<InterviewLogDO> pageResult = interviewLogService.getInterviewLogPage(pageVO);
        return success(InterviewLogConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出url访问记录 Excel")
    @PreAuthorize("@ss.hasPermission('zfkj:interview-log:export')")
    //@OperateLog(type = EXPORT)
    public void exportInterviewLogExcel(@Valid InterviewLogExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InterviewLogDO> list = interviewLogService.getInterviewLogList(exportReqVO);
        // 导出 Excel
        List<InterviewLogExcelVO> datas = InterviewLogConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "url访问记录.xls", "数据", InterviewLogExcelVO.class, datas);
    }

}
